{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "import cv2\n",
    "import numpy as np\n",
    "import os\n",
    "# 分割后的图片的文件夹，以及拼接后要保存的文件夹\n",
    "pic_path = '../../IMG/result02/'\n",
    "pic_target = '../../IMG/'\n",
    "# 数组保存分割后图片的列数和行数，注意分割后图片的格式为x_x.jpg，x从1开始\n",
    "num_width_list = []\n",
    "num_lenght_list = []\n",
    "# 读取文件夹下所有图片的名称\n",
    "picture_names =  os.listdir(pic_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "outputs": [],
   "source": [
    "# 获取分割后图片的尺寸\n",
    "img_1_1 = cv2.imread(pic_path + '1_1.jpg')\n",
    "(width, length, depth) = img_1_1.shape"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "outputs": [],
   "source": [
    "# 分割名字获得行数和列数，通过数组保存分割后图片的列数和行数#%%\n",
    "for picture_name in picture_names:\n",
    "    num_width_list.append(int(picture_name.split(\"_\")[0]))\n",
    "    num_lenght_list.append(int((picture_name.split(\"_\")[-1]).split(\".\")[0]))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 142,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1000\n",
      "213\n"
     ]
    }
   ],
   "source": [
    "# 取其中的最大值\n",
    "num_width = max(num_width_list)\n",
    "num_length = max(num_lenght_list)\n",
    "print(num_length)\n",
    "print(num_width)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "outputs": [],
   "source": [
    "# 预生成拼接后的图片\n",
    "pixiv=10\n",
    "splicing_pic = np.zeros((num_width*width//pixiv, num_length*length//pixiv, depth))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "outputs": [],
   "source": [
    "# 循环复制\n",
    "a=1\n",
    "b=1\n",
    "for i in range(1, num_width+1,pixiv):\n",
    "    for j in range(1, num_length+1,pixiv):\n",
    "        img_part = cv2.imread(pic_path + '{}_{}.jpg'.format(i, j))\n",
    "        splicing_pic[width*(a-1) : width*a, length*(b-1) : length*b, :] = img_part\n",
    "        b+=1\n",
    "    a+=1\n",
    "    b=1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 145,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]\n",
      "\n",
      " [[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]\n",
      "\n",
      " [[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]\n",
      "\n",
      " ...\n",
      "\n",
      " [[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]\n",
      "\n",
      " [[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]\n",
      "\n",
      " [[255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  ...\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]\n",
      "  [255. 255. 255.]]]\n"
     ]
    }
   ],
   "source": [
    "print(splicing_pic)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[255 255 255]]]\n"
     ]
    }
   ],
   "source": [
    "print(img_part)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 147,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "done!!!\n"
     ]
    }
   ],
   "source": [
    "# 保存图片，大功告成\n",
    "cv2.imwrite(pic_target + 'result.jpg', splicing_pic)\n",
    "print(\"done!!!\")\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}